home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / GeometryByReference / GeometryByReferenceNIOBuffer.java.z / GeometryByReferenceNIOBuffer.java
Encoding:
Java Source  |  2003-08-08  |  14.9 KB  |  510 lines

  1. /*
  2.  *    @(#)GeometryByReferenceNIOBuffer.java 1.7 02/10/21 13:42:07
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.*;
  42. import java.awt.event.*;
  43. import com.sun.j3d.utils.applet.MainFrame;
  44. import com.sun.j3d.utils.geometry.*;
  45. import com.sun.j3d.utils.universe.*;
  46. import javax.media.j3d.*;
  47. import javax.vecmath.*;
  48. import javax.swing.*;
  49. import javax.swing.event.*;
  50. import javax.swing.border.*;
  51. import com.sun.j3d.utils.behaviors.vp.*;
  52. import java.nio.*;
  53.  
  54. public class GeometryByReferenceNIOBuffer extends JApplet implements ActionListener,
  55. GeometryUpdater {
  56.  
  57.     RenderingAttributes ra;
  58.     ColoringAttributes ca;
  59.     Material mat;               
  60.     Appearance app;               
  61.     JComboBox geomType;
  62.     JComboBox vertexType;
  63.     JComboBox colorType;
  64.     JCheckBox transparency;
  65.     JComboBox updates;
  66.     Shape3D shape;               
  67.     TransparencyAttributes transp;
  68.     int updateIndex = 0;
  69.     int colorCount = 0, vertexCount = 0;
  70.     int vertexIndex = 0, colorIndex = 0;
  71.     
  72.     GeometryArray tetraRegular, tetraStrip, tetraIndexed, tetraIndexedStrip;
  73.     GeometryArray[] geoArrays = new GeometryArray[4];           
  74.  
  75.     private static final float sqrt3 = (float) Math.sqrt(3.0);
  76.     private static final float sqrt3_3 = sqrt3 / 3.0f;
  77.     private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;
  78.  
  79.     private static final float ycenter = 0.5f * sqrt24_3;
  80.     private static final float zcenter = -sqrt3_3;
  81.  
  82.     private static final Point3f p1 = 
  83.     new Point3f(-1.0f, -ycenter, -zcenter);
  84.     private static final Point3f p2 = 
  85.     new Point3f(1.0f, -ycenter, -zcenter);
  86.     private static final Point3f p3 =
  87.     new Point3f(0.0f, -ycenter, -sqrt3 - zcenter);
  88.     private static final Point3f p4 =
  89.     new Point3f(0.0f, sqrt24_3 - ycenter, 0.0f);
  90.  
  91.     private static final float[] floatVerts = {
  92.     p1.x, p1.y, p1.z, // front face
  93.     p2.x, p2.y, p2.z,
  94.     p4.x, p4.y, p4.z,
  95.     
  96.     p1.x, p1.y, p1.z,// left, back face
  97.     p4.x, p4.y, p4.z,
  98.     p3.x, p3.y, p3.z,
  99.     
  100.     p2.x, p2.y, p2.z,// right, back face
  101.     p3.x, p3.y, p3.z,
  102.     p4.x, p4.y, p4.z,
  103.     
  104.     p1.x, p1.y, p1.z,// bottom face
  105.     p3.x, p3.y, p3.z,
  106.     p2.x, p2.y, p2.z,
  107.     };
  108.     
  109.     private static final Color3f c1 = new Color3f(0.6f, 0.0f, 0.0f);
  110.     private static final Color3f c2 = new Color3f(0.0f, 0.6f, 0.0f);
  111.     private static final Color3f c3 = new Color3f(0.0f, 0.6f, 0.6f);
  112.     private static final Color3f c4 = new Color3f(0.6f, 0.6f, 0.0f);
  113.  
  114.  
  115.     
  116.     private static final float[] floatClrs = {
  117.     c1.x, c1.y, c1.z, // front face
  118.     c2.x, c2.y, c2.z,
  119.     c4.x, c4.y, c4.z,
  120.     
  121.     c1.x, c1.y, c1.z,// left, back face
  122.     c4.x, c4.y, c4.z,
  123.     c3.x, c3.y, c3.z,
  124.     
  125.     c2.x, c2.y, c2.z,// right, back face
  126.     c3.x, c3.y, c3.z,
  127.     c4.x, c4.y, c4.z,
  128.     
  129.     c1.x, c1.y, c1.z,// bottom face
  130.     c3.x, c3.y, c3.z,
  131.     c2.x, c2.y, c2.z,
  132.     };    
  133.  
  134.     private static final float[] indexedFloatVerts = {
  135.     p1.x,p1.y,p1.z,
  136.     p2.x,p2.y,p2.z,
  137.     p3.x,p3.y,p3.z,
  138.     p4.x,p4.y,p4.z,
  139.  
  140.     };
  141.  
  142.     
  143.     private static final float[] indexedFloatClrs = {
  144.     c1.x,c1.y,c1.z,
  145.     c2.x,c2.y,c2.z,
  146.     c3.x,c3.y,c3.z,
  147.     c4.x,c4.y,c4.z,
  148.     };
  149.     
  150.     private static final int[] indices = {0,1,3,0,3,2,1,2,3,0,2,1};
  151.     private int[] stripVertexCounts = {3,3,3,3};
  152.  
  153.     private SimpleUniverse u;
  154.  
  155.     private J3DBuffer floatBufferCoord;
  156.     private J3DBuffer floatBufferColor;
  157.     private J3DBuffer indexedFloatBufferCoord;
  158.     private J3DBuffer indexedFloatBufferColor;
  159.  
  160.     void createJ3DBuffers() {
  161.     int i;
  162.     ByteOrder order =  ByteOrder.nativeOrder();
  163.     
  164.     FloatBuffer coord = ByteBuffer.allocateDirect(36 * 4).order(order).asFloatBuffer();
  165.     coord.put(floatVerts, 0, 36);
  166.     floatBufferCoord = new J3DBuffer(coord);
  167.  
  168.     FloatBuffer color = ByteBuffer.allocateDirect(36 * 4).order(order).asFloatBuffer();
  169.     color.put(floatClrs, 0, 36);
  170.     floatBufferColor = new J3DBuffer(color);
  171.  
  172.     FloatBuffer indexedCoord = ByteBuffer.allocateDirect(12 * 4).order(order).asFloatBuffer();
  173.     indexedCoord.put(indexedFloatVerts, 0, 12);
  174.     indexedFloatBufferCoord = new J3DBuffer(indexedCoord);
  175.  
  176.     FloatBuffer indexedColor = ByteBuffer.allocateDirect(12 * 4).order(order).asFloatBuffer();
  177.     indexedColor.put(indexedFloatClrs, 0, 12); 
  178.     indexedFloatBufferColor = new J3DBuffer(indexedColor);
  179.     }
  180.     
  181.     BranchGroup createSceneGraph() {
  182.     BranchGroup objRoot = new BranchGroup();
  183.  
  184.     // Set up attributes to render lines
  185.         app = new Appearance();
  186.  
  187.     transp = new TransparencyAttributes();
  188.     transp.setTransparency(0.5f);
  189.     transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
  190.     transp.setTransparencyMode(TransparencyAttributes.NONE);
  191.     app.setTransparencyAttributes(transp);
  192.  
  193.     //create the direct nio buffer
  194.     createJ3DBuffers();
  195.  
  196.     tetraRegular = createGeometry(1);
  197.     tetraStrip =createGeometry(2);
  198.     tetraIndexed = createGeometry(3);
  199.     tetraIndexedStrip = createGeometry(4);
  200.  
  201.     geoArrays[0] = tetraRegular;
  202.     geoArrays[1] = tetraStrip;
  203.     geoArrays[2] = tetraIndexed;
  204.     geoArrays[3] = tetraIndexedStrip;
  205.     
  206.     shape = new Shape3D(tetraRegular, app);
  207.     shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
  208.     shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
  209.  
  210.     Transform3D t = new Transform3D();
  211.     // move the object upwards
  212.     t.set(new Vector3f(0.0f, 0.3f, 0.0f));
  213.  
  214.     // rotate the shape
  215.     Transform3D temp = new Transform3D();
  216.         temp.rotX(Math.PI/4.0d);
  217.     t.mul(temp);
  218.         temp.rotY(Math.PI/4.0d);
  219.         t.mul(temp);
  220.     
  221.     // Shrink the object 
  222.     t.setScale(0.6);
  223.  
  224.     TransformGroup trans = new TransformGroup(t);
  225.     trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  226.     trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  227.  
  228.     objRoot.addChild(trans);
  229.     trans.addChild(shape);
  230.  
  231.     BoundingSphere bounds =
  232.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  233.  
  234.     // Set up the global lights
  235.     Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
  236.     Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, -1.0f);
  237.     Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
  238.  
  239.     AmbientLight aLgt = new AmbientLight(alColor);
  240.     aLgt.setInfluencingBounds(bounds);
  241.     DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
  242.     lgt1.setInfluencingBounds(bounds);
  243.     objRoot.addChild(aLgt);
  244.     objRoot.addChild(lgt1);
  245.     
  246.     // Let Java 3D perform optimizations on this scene graph.
  247.         objRoot.compile();
  248.  
  249.     return objRoot;
  250.     }
  251.  
  252.     JPanel createGeometryByReferencePanel() {
  253.     JPanel panel = new JPanel();
  254.     panel.setBorder(new TitledBorder("Geometry Type"));
  255.  
  256.     String values[] = {"Array", "Strip", "Indexed", "IndexedStrip"};
  257.     geomType = new JComboBox(values);
  258.     geomType.setLightWeightPopupEnabled(false);
  259.     geomType.addActionListener(this);
  260.     geomType.setSelectedIndex(0);
  261.     panel.add(new JLabel("Geometry Type"));     
  262.     panel.add(geomType);
  263.  
  264.     return panel;
  265.     }
  266.  
  267.     JPanel createUpdatePanel() {
  268.  
  269.     JPanel panel = new JPanel();
  270.     panel.setBorder(new TitledBorder("Other Attributes"));
  271.  
  272.     String updateComp[] = { "None","Geometry", "Color"};
  273.     
  274.     transparency = new JCheckBox("EnableTransparency", 
  275.                       false);
  276.     transparency.addActionListener(this);
  277.     panel.add(transparency);
  278.  
  279.  
  280.     updates = new JComboBox(updateComp);
  281.     updates.setLightWeightPopupEnabled(false);
  282.     updates.addActionListener(this);
  283.     updates.setSelectedIndex(0);
  284.     panel.add(new JLabel("UpdateData"));     
  285.     panel.add(updates);
  286.  
  287.     return panel;
  288.     }
  289.  
  290.     
  291.  
  292.     public GeometryByReferenceNIOBuffer() {
  293.     }
  294.  
  295.     public void init() {
  296.     Container contentPane = getContentPane();
  297.     
  298.         Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
  299.         contentPane.add("Center", c);
  300.  
  301.         BranchGroup scene = createSceneGraph();
  302.         // SimpleUniverse is a Convenience Utility class
  303.         u = new SimpleUniverse(c);
  304.  
  305.     // add mouse behaviors to the viewingPlatform
  306.     ViewingPlatform viewingPlatform = u.getViewingPlatform();
  307.  
  308.         // This will move the ViewPlatform back a bit so the
  309.         // objects in the scene can be viewed.
  310.         viewingPlatform.setNominalViewingTransform();
  311.         u.addBranchGraph(scene);
  312.  
  313.     // add Orbit behavior to the ViewingPlatform
  314.     OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
  315.     BoundingSphere bounds =
  316.         new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  317.     orbit.setSchedulingBounds(bounds);
  318.     viewingPlatform.setViewPlatformBehavior(orbit);
  319.  
  320.     // Create GUI
  321.     JPanel p = new JPanel();
  322.     BoxLayout boxlayout = new BoxLayout(p, 
  323.                         BoxLayout.Y_AXIS);
  324.     p.add(createGeometryByReferencePanel());
  325.     p.add(createUpdatePanel());
  326.     p.setLayout(boxlayout);
  327.  
  328.     contentPane.add("South", p);
  329.     }
  330.  
  331.     public void destroy() {
  332.     u.cleanup();
  333.     }
  334.  
  335.     public void actionPerformed(ActionEvent e) {
  336.     Object target = e.getSource();
  337.     GeometryArray geo;
  338.     boolean setColor = false, setVertex = false;
  339.     if (target == geomType) {
  340.         geo = geoArrays[geomType.getSelectedIndex()];
  341.         // Set everything to null, and set it later ..
  342.         geo.setColorRefBuffer(null);
  343.         geo.setCoordRefBuffer(null);
  344.         shape.setGeometry(geoArrays[geomType.getSelectedIndex()]);
  345.  
  346.         setColor = true;
  347.         setVertex= true;
  348.  
  349.         
  350.     } 
  351.     else if (target == transparency) {
  352.         if (transparency.isSelected()) {
  353.         transp.setTransparencyMode(TransparencyAttributes.BLENDED);
  354.         }
  355.         else {
  356.         transp.setTransparencyMode(TransparencyAttributes.NONE);
  357.         }
  358.  
  359.     }
  360.     else if (target == updates) {
  361.         updateIndex = updates.getSelectedIndex();
  362.         if (updateIndex == 1) {
  363.         System.out.println("Doing coordinate update");
  364.         ((GeometryArray)(shape.getGeometry())).updateData(this);
  365.         }
  366.         else if (updateIndex == 2) {
  367.         System.out.println("Doing color update");
  368.         ((GeometryArray)(shape.getGeometry())).updateData(this);
  369.         }
  370.  
  371.     }
  372.  
  373.     if (setVertex) {
  374.         geo = (GeometryArray) shape.getGeometry();
  375.         if (geo instanceof IndexedGeometryArray)
  376.         geo.setCoordRefBuffer(indexedFloatBufferCoord);
  377.         else
  378.         geo.setCoordRefBuffer(floatBufferCoord);
  379.         
  380.     }
  381.     if (setColor) {
  382.         geo = (GeometryArray) shape.getGeometry();
  383.         if (geo instanceof IndexedGeometryArray)
  384.         geo.setColorRefBuffer(indexedFloatBufferColor);
  385.         else
  386.         geo.setColorRefBuffer(floatBufferColor);
  387.     }
  388.     }
  389.  
  390.  
  391.                
  392.     public static void main(String[] args) {
  393.     Frame frame = new MainFrame(new GeometryByReferenceNIOBuffer(), 800, 800);
  394.     }
  395.                
  396.     public GeometryArray createGeometry (int type) {
  397.     GeometryArray tetra = null;
  398.     if (type == 1) {
  399.         tetra =new TriangleArray(12, 
  400.                      TriangleArray.COORDINATES|
  401.                      TriangleArray.COLOR_3|
  402.                      TriangleArray.BY_REFERENCE|
  403.                      TriangleArray.USE_NIO_BUFFER);
  404.         
  405.         tetra.setCoordRefBuffer(floatBufferCoord);
  406.         tetra.setColorRefBuffer(floatBufferColor);
  407.  
  408.     }
  409.     else if (type == 2) {
  410.         tetra = new TriangleStripArray(12,
  411.                        TriangleStripArray.COORDINATES|
  412.                        TriangleStripArray.COLOR_3|
  413.                        TriangleStripArray.BY_REFERENCE|
  414.                        TriangleStripArray.USE_NIO_BUFFER,
  415.                        stripVertexCounts);
  416.         tetra.setCoordRefBuffer(floatBufferCoord);
  417.         tetra.setColorRefBuffer(floatBufferColor);
  418.         
  419.     }
  420.     else if (type == 3) { // Indexed Geometry
  421.         tetra = new IndexedTriangleArray(4,
  422.                          IndexedTriangleArray.COORDINATES|
  423.                          IndexedTriangleArray.COLOR_3|
  424.                          IndexedTriangleArray.BY_REFERENCE|
  425.                          IndexedTriangleArray.USE_NIO_BUFFER,
  426.                          //IndexedTriangleStripArray.USE_COORD_INDEX_ONLY,
  427.                          12);
  428.         tetra.setCoordRefBuffer(indexedFloatBufferCoord);
  429.         tetra.setColorRefBuffer(indexedFloatBufferColor);
  430.         ((IndexedTriangleArray)tetra).setCoordinateIndices(0, indices);
  431.         ((IndexedTriangleArray)tetra).setColorIndices(0, indices);
  432.     }
  433.     else if (type == 4) { // Indexed strip geometry
  434.         tetra = new IndexedTriangleStripArray(4,
  435.                           IndexedTriangleStripArray.COORDINATES|
  436.                           IndexedTriangleStripArray.COLOR_3|
  437.                           IndexedTriangleStripArray.BY_REFERENCE|
  438.                           IndexedTriangleStripArray.USE_NIO_BUFFER|
  439.                           IndexedTriangleStripArray.USE_COORD_INDEX_ONLY,
  440.                           12,
  441.                           stripVertexCounts);
  442.         tetra.setCoordRefBuffer(indexedFloatBufferCoord);
  443.         tetra.setColorRefBuffer(indexedFloatBufferColor);
  444.         ((IndexedTriangleStripArray)tetra).setCoordinateIndices(0, indices);
  445.         ((IndexedTriangleStripArray)tetra).setColorIndices(0, indices);
  446.     }
  447.  
  448.     if (tetra != null)
  449.         tetra.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);
  450.     return tetra;
  451.     }
  452.     
  453.     public void updateData(Geometry geometry) {
  454.     int i;
  455.     float val;
  456.     float val1;
  457.     if (updateIndex == 1) { // geometry
  458.         // Translate the geometry by a small amount in x
  459.         vertexCount++;
  460.         if ((vertexCount &1) == 1)
  461.         val = 0.2f;
  462.         else
  463.         val = -0.2f;
  464.  
  465.         FloatBuffer indexedCoord = (FloatBuffer)indexedFloatBufferCoord.getBuffer();
  466.         indexedCoord.rewind();
  467.         FloatBuffer coord = (FloatBuffer)floatBufferCoord.getBuffer();
  468.         coord.rewind();
  469.         
  470.         if (vertexIndex == 0) {
  471.         // Do Indexed geometry
  472.         for (i = 0; i < indexedCoord.limit(); i+=3) {
  473.             val1 = indexedCoord.get(i);
  474.             indexedCoord.put(i, val1 + val);
  475.         }
  476.         // Do non-indexed float geometry
  477.         for (i = 0; i < coord.limit(); i+=3) {
  478.             val1 = coord.get(i);
  479.             coord.put(i, val1 + val);
  480.         }
  481.         }
  482.     }
  483.     else if (updateIndex == 2) { // colors
  484.         colorCount++;
  485.         if ((colorCount & 1) == 1)
  486.         val = 0.4f;
  487.         else
  488.         val = -0.4f;
  489.  
  490.         FloatBuffer indexedColors = (FloatBuffer)indexedFloatBufferColor.getBuffer();
  491.         indexedColors.rewind();
  492.         FloatBuffer colors = (FloatBuffer)floatBufferColor.getBuffer();
  493.         colors.rewind();
  494.         
  495.         if (colorIndex == 0) {
  496.         // Do Indexed geometry
  497.         for (i = 0; i < indexedColors.limit(); i+=3) {
  498.             indexedColors.put(i, indexedColors.get(i) + val);
  499.         }
  500.         // Do non-indexed float geometry
  501.         for (i = 0; i < colors.limit(); i+=3) {
  502.             colors.put(i, colors.get(i) + val);
  503.         }
  504.         }
  505.  
  506.     }
  507.  
  508.     }
  509. }
  510.